//
//  DBHomeSmallCardCell.swift
//  DBCA
//
//  Created by mugua on 2024/7/4.
//

import UIKit


class DBHomeSmallCardCell: UITableViewCell {
    
    let disposeBag = DisposeBag()
    let buttonTap = PublishSubject<Void>()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        contentView.addSubview(bgImgView)
        contentView.addSubview(centerBgImgView)
        contentView.addSubview(centerRightBgImgView)
        centerBgImgView.addSubview(maxMoneyTipLabel)
        centerBgImgView.addSubview(moneyLabel)
        centerBgImgView.addSubview(loanTermImgView)
        centerBgImgView.addSubview(loanTermLabel)
        centerBgImgView.addSubview(loanTermTipLabel)
        centerBgImgView.addSubview(interestTermImgView)
        centerBgImgView.addSubview(interestTermLabel)
        centerBgImgView.addSubview(interestTermTipLabel)
        contentView.addSubview(btn)
        
        bgImgView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(290)
        }
        
        
        centerBgImgView.snp.makeConstraints { make in
            make.top.equalTo(129)
            make.centerX.equalToSuperview()
            make.width.equalTo(335)
            make.height.equalTo(197)
        }
        
        centerRightBgImgView.snp.makeConstraints { make in
            make.right.equalTo(centerBgImgView)
            make.top.equalTo(88)
        }
        
        maxMoneyTipLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(20)
            make.height.equalTo(20)
        }
        
        moneyLabel.snp.makeConstraints { make in
            make.top.equalTo(57)
            make.left.equalTo(20)
            make.height.equalTo(40)
        }
        
        loanTermImgView.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.bottom.equalTo(-23)
        }
        
        loanTermLabel.snp.makeConstraints { make in
            make.centerY.equalTo(loanTermImgView)
            make.left.equalTo(loanTermImgView.snp.right).offset(2)
            make.height.equalTo(20)
        }
        
        loanTermTipLabel.snp.makeConstraints { make in
            make.top.equalTo(120)
            make.left.equalTo(loanTermImgView)
            make.height.equalTo(20)
        }
        
        interestTermImgView.snp.makeConstraints { make in
            make.left.equalTo(182)
            make.centerY.equalTo(loanTermImgView)
        }
        
        interestTermLabel.snp.makeConstraints { make in
            make.centerY.equalTo(interestTermImgView)
            make.left.equalTo(interestTermImgView.snp.right).offset(2)
            make.height.equalTo(20)
        }
        
        interestTermTipLabel.snp.makeConstraints { make in
            make.top.equalTo(120)
            make.left.equalTo(interestTermImgView)
            make.height.equalTo(20)
        }
        
        btn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(centerBgImgView.snp.bottom).offset(15)
            make.left.equalTo(46)
            make.right.equalTo(-46)
            make.height.equalTo(56)
        }
        
    }
    
    var model: DBHomeWorkinModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            moneyLabel.text = model.table
            maxMoneyTipLabel.text = model.weaker
            loanTermTipLabel.text = model.clear
            interestTermTipLabel.text = model.chest
            btn.setTitle(model.indian, for: .normal)
            
            if let money = model.table {
                if money.contains("R") {
                    let tableArr = money.components(separatedBy: " ")
                    let att = NSMutableAttributedString(string: money)
                    att.addAttribute(.font, value: DBFuturaMaxiStd_DemiFont(24), range: tableArr.first!.rangeOfAll())
                    moneyLabel.attributedText = att
                }
            }
            
            if let loanTerm = model.played {
                let tableArr = loanTerm.components(separatedBy: " ")
                let att = NSMutableAttributedString(string: loanTerm)
                att.addAttribute(.font, value: DBFuturaMaxiStd_DemiFont(20), range: tableArr.first!.rangeOfAll())
                loanTermLabel.attributedText = att
            }
            
            if let interestTerm = model.gate {
                let tableArr = interestTerm.components(separatedBy: " ")
                let att = NSMutableAttributedString(string: interestTerm)
                att.addAttribute(.font, value: DBFuturaMaxiStd_DemiFont(20), range: tableArr.first!.rangeOfAll())
                interestTermLabel.attributedText = att
            }
        }
    }
    
    
    
    private lazy var bgImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_big_card_bg")
        return imageView
    }()
    
    private lazy var centerBgImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_small_card_center_bg")
        return imageView
    }()
    
    private lazy var centerRightBgImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_small_card_center_bg_right")
        return imageView
    }()
    
    private lazy var maxMoneyTipLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBTextColor
        label.font = DBFuturaMaxiStd_DemiFont(14)
        label.text = "Maximum Amount"
        return label
    }()
    
    private lazy var moneyLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBViewBgColor
        label.font = DBFuturaMaxiStd_DemiFont(40)
        label.text = "900,000"
        return label
    }()
    
    private lazy var loanTermImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_small_card_loan_term")
        return imageView
    }()
    
    private lazy var interestTermImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_small_card_interest_term")
        return imageView
    }()
    
    private lazy var loanTermLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBViewBgColor
        label.font = DBFuturaMaxiStd_DemiFont(11)
        label.text = "91 hair"
        return label
    }()
    
    private lazy var interestTermLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBViewBgColor
        label.font = DBFuturaMaxiStd_DemiFont(11)
        label.text = "0.08% hair"
        return label
    }()
    
    private lazy var interestTermTipLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBTextColor
        label.font = DBFuturaMaxiStd_BookFont(12)
        label.text = "Interest Term"
        return label
    }()
    
    private lazy var loanTermTipLabel: UILabel = {
        let label = UILabel()
        label.textColor = DBTextColor
        label.font = DBFuturaMaxiStd_BookFont(12)
        label.text = "Loan Term"
        return label
    }()
    
    private lazy var btn: DBCustomButton = {
        let btn = DBCustomButton()
        btn.style = .titleLeftImageRight
        btn.space = 14
        btn.delayTime = 1
        btn.clipsToBounds = true
        btn.backgroundColor = DBViewBgColor
        btn.titleLabel?.font = DBFuturaMaxiStd_DemiFont(16)
        btn.setTitle("Segra Ambil", for: .normal)
        btn.setTitleColor(DBHexColor("CCFF00"), for: .normal)
        btn.setImage(UIImage(named: "home_big_card_apply"), for: .normal)
        btn.layer.cornerRadius = 2
        
        btn.rx
            .tap
            .bind(to: buttonTap)
            .disposed(by: disposeBag)
        return btn
    }()
    
}
